﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class ItemUIForm : BaseUIForm, IEventListener
{
    private GameObject description;
    private GameObject ItemImage;
    private string ItemSprite;
    private string ItemDescription;
    private GameObject Close;
    private void Awake()
    {
        CurrentUIType.UIForms_Type = UIFormType.PopUp;  //弹出窗体
        CurrentUIType.UIForm_LucencyType = UIFormLucenyType.Translucence;
        CurrentUIType.UIForms_ShowMode = UIFormShowMode.ReverseChange;
        //UI子物体
        description = transform.Find("Description").gameObject;
        ItemImage = transform.Find("Icon").gameObject;
        Close = transform.Find("Close").gameObject;
        Close.GetComponent<Button>().onClick.AddListener(OnClickClose);

    }
    private void Start()
    {
        EventNodeCore.GetInstance().AttachEventListener(-EventDef.PickCrystal, this);
    }
    public bool HandleEvent(int id, object param1, object param2)
    {
        if (id == -EventDef.PickCrystal)
        {
            Debug.Log("拾取到晶体");
            //Item data = (Item)param1;
            //ItemSprite = data.Sprite;
            string data = (string)param1;
            Debug.LogWarning(data);
            //ItemImage.GetComponent<Image>().overrideSprite = Resources.Load(ItemSprite, typeof(Sprite)) as Sprite;
            OpenUIForm(this.name);
            StartCoroutine(Print(data));
            RegistAction(1);
            //Time.timeScale = 0;
        }
        return false;
    }
    IEnumerator Print(string data)
    {
        string s = "";
        int i = 0;
        while (i < data.Length)
        {
            s += data[i].ToString();
            description.GetComponent<Text>().text = s;
            yield return new WaitForSeconds(0.05f);
            i++;
        }

    }
    public void OnClickClose()
    {
        RegistAction(-1);
        //Time.timeScale = 1;
        Debug.Log("close");
        GameManager.Instance.ProcessFuc();
        Hiding();
        description.GetComponent<Text>().text = "";
    }
    public int EventPriority()
    {
        return 0;
    }

    private void RegistAction(int IsR)
    {
        EventNodeCore.GetInstance().HandleEvent(EventDef.Action * IsR, null, null);
    }
}
